<template>
	<div class="com-index">
		<!-- 搜索栏 -->
		<div class="com-index-search">
			<slot name="search">
				<Search :showSearch="showSearch" :breadCrumb="breadCrumb" />
			</slot>
		</div>
		<div class="com-index-content">
			<template v-if="isInit">
				<!-- 过滤 -->
				<Filter ref="filterRef" v-if="showFilter" @update="updateFilter" :toggleView="toggleView">
					<slot name="filterForm"></slot>
				</Filter>
				<!-- 卡片和列表 -->
				<div class="list">
					<!-- 初始化 -->
					<template v-if="isInitList">
						<Card :dataList="dataList" :filterList="filterRef?.filterList" v-if="viewType === 0" />
						<slot name="table" :dataList="dataList" v-if="viewType === 1"></slot>
					</template>
					<!-- 自定义 -->
					<template v-else>
						<!-- <slot name="card" v-if="viewType === 0"></slot> -->
						<slot :dataList="dataList"></slot>
					</template>
				</div>
			</template>
			<slot name="content"></slot>
		</div>
		<!-- 新建弹窗 -->
		<slot name="add"></slot>
	</div>
</template>
<script lang="ts" setup>
const props = defineProps({
	getListFun: {
		type: Function,
		default: () => {
			return function () {};
		}
	}, // 数据
	isInitList: {
		type: Boolean,
		default: () => {
			return true;
		}
	}, // 是否使用初始化列表视图
	toggleView: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	showFilter: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	showSearch: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	params: {
		type: Object,
		default: () => {
			return {};
		}
	},
	// 是否初始化执行函数
	initApi: {
		type: Boolean,
		default: true
	},
	keywords: {
		type: String,
		default: () => {
			return "";
		}
	},
	breadCrumb: {
		//面包屑配置
		type: Object,
		default: () => null
	},
	isInit: { type: Boolean, default: () => { return true } }
});
import Search from "./Search.vue";
import Filter from "./Filter.vue";
import mitt from "@/utils/bus";
const emit = defineEmits(["search", "update:keywords"]);
const Card = defineAsyncComponent(() => import("./Card.vue"));
const filterRef = ref(null);
const listRef = ref(null);
const viewType = ref(0); // 视图方式  0阶段 1表格
const dataList = ref([]); // 列表
const isHaveData = ref(false);
const pageParams = ref({
	total: 0,
	pageNo: 1,
	pageSize: 10
});

watch(
	() => props.getListFun,
	(e) => {
		nextTick(() => {
			if (props.initApi) {
				getList();
			}
		});
	},
	{ immediate: true }
);
const updateFilter = (val) => {
	if (!Array.isArray(val)) return;
	const keywordsInfo = val.find((i) => i.filterType === "keywords");
	emit("update:keywords", keywordsInfo?.value);
	emit("search", val);
};
watch(
	() => filterRef?.value?.filterList,
	(val) => {
		if (val?.length > 0) {
			val.forEach((item) => {
				pageParams.value[item.keys] = item.value;
			});
			nextTick(() => {
				getList();
			});
		}
	},
	{ deep: true, immediate: true }
);

onBeforeUnmount(() => {
	mitt.all.clear(); // 销毁mitt
});

const loading = ref(false);
// 获取列表
const getList = async () => {
	dataList.value = [];
	loading.value = true;
	const { pageNo, pageSize } = pageParams.value;
	pageParams.value = {
		pageNo,
		pageSize,
		...filterRef?.value?.filterParams,
		...props.params
		// searchKeys: filterRef?.value?.filterList
	};
	try {
		delete pageParams.value.total;
		const res = await props.getListFun(pageParams.value);
		if (typeof res === "function") return;
		pageParams.value.total = res?.total || 0;
		dataList.value = viewType.value === 0 ? [...dataList.value, ...res] : res?.list || [];
		for (const k of dataList.value) {
			if (k?.list?.total >= 10) {
				isHaveData.value = true;
				break;
			}
		}
	} catch (err) {
		console.log(err);
	} finally {
		loading.value = false;
	}
};

// 获取子组件触发的方法
const getEmit = (val) => {
	console.log("val===", { ...val });
	const { emitType, value, keys } = val;
	if (emitType === "view") {
		// 切换视图
		viewType.value = value;
	}
	if (emitType === "loading") {
		// 切换视图
		if (isHaveData.value) {
			// pageParams.value.pageNo++;
			// getList();
		}
	} else if (emitType === "delete") {
		// 删除过滤条件
		if (value) {
			getList();
		}
	}
};

provide("getEmit", getEmit);
provide("getList", getList);

defineExpose({
	getList,
	filterRef,
	viewType,
	dataList,
	pageParams,
	loading
});
</script>
